<?php

namespace App\Http\Requests\School;

use App\Models\FoodType;
use Illuminate\Foundation\Http\FormRequest;

/**
 * Class EditFoodType
 * @package App\Http\Requests\School
 */
class EditFoodType extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->id) {
            return [
                'name' => [
                    'required',
                    function ($attribute, $value, $fail) {
                        if (FoodType::query()->where('status', 1)
                            ->where('school_id', \Auth::user()->school_id)
                            ->where('id', '<>', $this->id)
                            ->where('name', $value)->exists()) {
                            return $fail('已存在相同名称的菜品类型');
                        }
                    }
                ]
            ];
        }
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'name' => '菜品种类名称'
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '请填写:attribute'
        ];
    }
}
